const api = require('./api')
// const fs = require('fs')
// const path = require('path')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()
const http = require('http')
const WebSocket = require('ws')
const cors = require('cors')
app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(api)
// app.use(express.static(path.resolve(__dirname, '../dist')))
// app.get('*', function (req, res) {
//   const html = fs.readFileSync(path.resolve(__dirname, '../dist/index.html'), 'utf-8')
//   res.send(html)
// })
// const rs = fs.createReadStream('../static/logo.png')
// const ws = fs.createWriteStream('./a.png')
// rs.pipe(ws)
app.listen(8089)
console.log('success listen......')
const server = http.createServer(app)
const wss = new WebSocket.Server({server})

wss.on('connection', function connection (ws) {
  console.log('链接成功！')
  ws.on('message', function incoming (data) {
    console.log(data)
    wss.clients.forEach(function each (client) {
      client.send(data)
    })
  })
})
server.listen(8088)
